package com.bicon.botu.mall.order.api;

import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.order.dto.AfterSaleDetailDto;
import com.bicon.botu.mall.order.service.IAfterSalesRecordsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Map;

/**
 * <p>
 * 订单退款退货记录 前端控制器
 * </p>
 *
 * @author 李双喜
 * @since 2018-06-14
 */
@RestController
@RequestMapping("/V3.0.0/api/after-sales-records")
public class AfterSalesRecordsApi extends BaseController {
    @Autowired
    private IAfterSalesRecordsService iAfterSalesRecordsService;

    /**
     * 申请售后
     *
     * @param afterSalesParamJson json字符串
     * @return
     */
    @PostMapping("/apply-sales")
    public BaseResult applyAfterSales(@RequestParam String afterSalesParamJson) {
        if (StringUtils.isEmpty(afterSalesParamJson)) {
            new BaseResult(400, "参数不能为空");
        }
        String result = iAfterSalesRecordsService.applyAfterSales(afterSalesParamJson);
        if (StringUtils.isNotEmpty(result)) {
            return new BaseResult(200, "success", result);
        }
        return new BaseResult(500, "error");
    }

    /**
     * 计算退费金额
     *
     * @param orderChildIds 多个子订单id  逗号隔开
     * @return
     */
    @RequestMapping(value = "/return-premium", method = RequestMethod.POST)
    public BaseResult calcPayBackAmount(@RequestParam String orderChildIds) {
        if (null == orderChildIds) {
            new BaseResult(400, "参数不能为空");
        }
        Map<String, BigDecimal> map = iAfterSalesRecordsService.calcPayBackAmount(orderChildIds);
        return new BaseResult(200, "success", map);
    }


    /**
     * 根据id查询详情
     *
     * @param id 申请售后id
     * @return
     */
    @PostMapping("/for-details")
    public BaseResult forDetails(@RequestParam String id) throws ParseException {
        if (StringUtils.isEmpty(id)) {
            new BaseResult(400, "参数不能为空");
        }
        AfterSaleDetailDto result = iAfterSalesRecordsService.forDetails(id);
        return new BaseResult(200, "success", result);

    }


    /**
     * 撤销申请
     *
     * @param id 申请售后id
     * @return
     */
    @PostMapping("/for-repeal")
    public BaseResult forRepeal(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) {
            new BaseResult(400, "参数不能为空");
        }
        String result = iAfterSalesRecordsService.forRepeal(id);
        return new BaseResult(200, "success", result);

    }

    /**
     * 修改申请售后
     */
    @PostMapping("/for-update")
    public BaseResult modifyAfterSales(@RequestParam String afterSalesParamJson) {
        if (StringUtils.isEmpty(afterSalesParamJson)) {
            new BaseResult(400, "参数不能为空");
        }
        String result = iAfterSalesRecordsService.modifyAfterSales(afterSalesParamJson);
        return new BaseResult(200, "success", result);

    }

    /**
     * 退货发货单填写
     *
     * @param afterSalesId         申请退款id
     * @param deliveryPointId      物流公司id
     * @param logisticsCode        运单号
     * @param logisticsDescription 物流描述
     * @return
     */
    @PostMapping("/for-logistics")
    public BaseResult commitLogistics(@RequestParam String afterSalesId, @RequestParam String deliveryPointId, @RequestParam String logisticsCode, String logisticsDescription) {
        if (StringUtils.isAnyBlank(afterSalesId, deliveryPointId, logisticsCode)) {
            new BaseResult(400, "参数不能为空");
        }
        Boolean result = iAfterSalesRecordsService.commitLogistics(afterSalesId, deliveryPointId, logisticsCode, logisticsDescription);
        if (result) {
            return new BaseResult(200, "success", result);
        }
        return new BaseResult(500, "error");
    }

    /**
     * 根据售后订单号，查询物流信息
     *
     * @param afterSalesId 申请售后id
     * @return
     */
    @RequestMapping(value = "/logistics", method = RequestMethod.GET)
    public BaseResult getLogistics(@RequestParam("afterSalesId") String afterSalesId) {
        return sendResult200(iAfterSalesRecordsService.getLogistics(afterSalesId));
    }


}
